#include "infoview.h"
#include "ui_infoview.h"

InfoView::InfoView(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::InfoView)
{
    ui->setupUi(this);

    table=INFO_FILE;
    systemModel=new QSqlTableModel(this);
    ui->systemView->setEditTriggers(QTableView::NoEditTriggers);
    ui->systemView->setModel(systemModel);

    updateView();
    createMenu();
}

InfoView::~InfoView()
{
    delete ui;
}

void InfoView::generateMenu()
{

}

void InfoView::updateView()
{
    systemModel->setTable(table);
    systemModel->setFilter(QString("category='%1'").arg("system"));
    systemModel->select();

    ui->systemView->hideColumn(0);   //id
    ui->systemView->hideColumn(1);  //category
    systemModel->setHeaderData(2,Qt::Horizontal,"label");   //label
    ui->systemView->hideColumn(3);   //detail
    systemModel->setHeaderData(4,Qt::Horizontal,"distro");   //addition
}

void InfoView::loadFile(const QString &fileName)
{
    if(!QFile::exists(fileName))
    {
        ui->location->setText(fileName + " 不存在");
        ui->content->clear();
        return;
    }

    ui->location->setText(fileName);
    QFile file(fileName);
    if(file.open(QFile::ReadOnly|QFile::Text))
    {
        QTextStream text(&file);
        QString content=text.readAll();
        file.close();
        if(!content.isEmpty())
            ui->content->setPlainText(content);
    }
}

void InfoView::on_systemView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QString detail=index.sibling(index.row(),3).data().toString();
        loadFile(detail);
    }
}
